<?php
namespace Site\Controller;
use Common\Controller\AdminbaseController;
class RoleController extends AdminbaseController 
{   
	protected $roleObj;
    public function _initialize()
    {
          parent::_initialize();
          $this->roleObj = D("User_role");
    }

	public function index()
	{
		$this->lists();
	}

	public function lists()
	{
	    $count      = $this->roleObj->count();
        $Page       = new \Think\Page($count,20);
        $show       = $Page->show();
        $list =  $this->roleObj->order(array("id" => "ASC"))->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show); 
        $this->display('list');
	}

    public function menuTree($parentid=0,$level=0,$html='|── ')
    {
        static $tree = array();
        $menuList = $this->getMenuList($parentid);
        if (!empty($menuList)) 
        {
            foreach($menuList as $menu)
            {
                $menu['sort'] = $level;
                $html=str_repeat($html,$level);
                $menu['html'] = $html;
                $tree[] = $menu;
                $parentId = $menu['id'];
                $this->menuTree($parentId,$level+1);
            }
        }
        return $tree;
    }

    public function getMenuList($parentId)
    {
        $menuList = $this->menuObj->where(array("parentid" => $parentId))->order(array("listorder" => "ASC"))->select();
        return $menuList;
    }

    public function add()
    {
        $menuObjs=$this->menuTree();
        $this->assign("menus",$menuObjs);
    	$this->display("add");
    }
    public function edit($id=0)
    {
    	if ($id>0) 
    	{
            $menuObjs=$this->menuTree();
            $this->assign("menus",$menuObjs);
	    	$roleObj = $this->roleObj->find($id);
            $this->assign("role",$roleObj);
            $this->display("edit");
    	}else{
            header("404");
        }
    }
    public function save()
    {
        $id = I('post.inputId','-1','htmlspecialchars');
        $name = I('post.inputName','','htmlspecialchars');
        $menuids = I('post.inputMenuids','','htmlspecialchars');
        $menuidsStr="";
        if(!empty($menuids)) $menuidsStr=implode(',', $menuids);
        $data = array();
        $data["name"]  = $name;
        $data["menuids"]  = $menuidsStr;
        if($id>0)
        {
            $editResult = $this->roleObj->where("id=%d",$id)->save($data);
            $arr = array("status"=>'1',"message"=>"编辑用户角色成功","url"=>"index");
        }
        else
        {
           $data['createtime'] =time();
           $addResult = $this->roleObj->add($data);
            if($addResult) {
                $arr = array("status"=>'1',"message"=>"添加用户角色成功","url"=>"index");
            }else{
                $arr = array("status"=>'0',"message"=>"添加用户角色失败");
            }
        }
        echo json_encode($arr);
    }
   /*单个删除 id*/
    public function delete($id)
    {
        parent::_delete($this->roleObj,$id);
    }

    /* 批量删除 ids*/
    public function checkDeletes($ids)
    {
      parent::_checkDeletes($this->roleObj,$ids);
    }
    /* 单个修改当前状态status 0 1 -1*/
    public function changeStatus($id,$status)
    {
        parent::_changeStatus($this->roleObj,$id,$status);
    }
    /*批量 修改当前状态status 0 1 -1*/
    public function checkChanges($status,$ids)
    {
      parent::_checkChanges($this->roleObj,$status,$ids);
    }
    // 检查用户名是否已存在
    public function validName(){
      $type =  $_POST["param"];
      $map['type'] = $type;
      $result = $Blog_type->where($map)->select();
      if(empty($result)){
        echo "y";
      }else{
        echo "<i class='icon-warning-sign'></i> 此标签已存在";
      }
    }
    
}